package org.skatiger.jrove.spring.tags.renderer;

import java.io.IOException;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.UIComponent;
import org.skatiger.jrove.renderer.Renderer;
import org.skatiger.jrove.spring.tags.component.BindErrors;
import org.springframework.validation.Errors;

public class BindErrorsRenderer extends Renderer {
    @Override
    public boolean isRendersChildren() {
        return true;
    }

    @Override
    public void encodeChildren(ViewContext context, UIComponent component)
            throws IOException {
        super.encodeChildren(context, component);
        BindErrors bindErrors = (BindErrors) component;
        Errors errors = bindErrors.getErrors();
        if (errors != null && errors.hasErrors()) {
            bindErrors.capturePreivousValue(context);
            String var = bindErrors.getVar();
            context.setAttribute(var, errors);
            
            super.encodeChildren(context, component);
            
            context.removeAttribute(var);
            bindErrors.restorePreivousValue(context);
        }
    }
}
